﻿using Autodesk.Aec.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using DBObject = Autodesk.AutoCAD.DatabaseServices.DBObject;
using Entity = Autodesk.AutoCAD.DatabaseServices.Entity;

namespace ChangDiTools {
    public static class SelectionTools {
        /// <summary>
        /// 选择过一点的所有实体
        /// </summary>
        /// <param name="ed">命令行对象</param>
        /// <param name="point">点</param>
        /// <param name="filter">选择过滤器</param>
        /// <returns>返回过指定点的所有实体</returns>
        public static PromptSelectionResult SelectAtPoint(this Editor ed, Point3d pt) {
            return ed.SelectCrossingWindow(pt, pt);
        }
        /// <summary>
        /// 选择过一点的所有实体
        /// </summary>
        /// <param name="ed">命令行对象</param>
        /// <param name="point">点</param>
        /// <returns>返回过指定点的所有实体</returns>
        public static PromptSelectionResult SelectAtPoint(this Editor ed, Point3d pt, SelectionFilter sf) {
            return ed.SelectCrossingWindow(pt, pt, sf);
        }
        public static Entity SelectEntity(string infomation) {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor editor = doc.Editor;
            Entity entity = null;
            PromptEntityResult re = editor.GetEntity(infomation);
            if (re.Status == PromptStatus.OK) {
                using (Transaction transaction = db.TransactionManager.StartTransaction()) {

                    DBObject dbObject = transaction.GetObject(re.ObjectId, OpenMode.ForWrite, true);
                    //editor.WriteMessage("\n DBOBject=======" + dbObject.ToString());
                    entity = (Entity)dbObject;
                    //Application.ShowAlertDialog("Handle=========" + entity.ObjectId.Handle);
                    transaction.Commit();
                }
            }
            return entity;
        }
    }
}
